///<reference path="DateTimeUtils.ts" />
///<reference path="ByteArray.ts" />
import DateTimeUtils=utils.DateTimeUtils;
import ByteArray=utils.ByteArray;

namespace demo{
    export class DemoClass{
        
        constructor(){

        }
                
        public get numMax40():number{
            return 101;
        }

        public get numMin():number{
            return 101;
        }
    }
}


function fmtDHIS(fmt: string,second:number=-1,d:number=-1,h:number=-1,i:number=-1,s:number=-1,isSlice:boolean=false): string{
    let d_REG: RegExp = /d+/g; //天
    let h_REG: RegExp = /h+/g;
    let i_REG: RegExp = /i+/g;
    let s_REG: RegExp = /s+/g;

    let regArr:RegExp[] = [d_REG,h_REG,i_REG,s_REG];
    let secs:number[]= [86400,3600,60,0];   
    let dhis:number[] = [d,h,i,s];
    for(let i:number=0;i<regArr.length;i++){
        regArr[i].lastIndex = 0;
        let reg:RegExpExecArray = regArr[i].exec(fmt);
        if(reg){
            let tar:number = dhis[i];//直接传参替换    
            if(second>-1 && tar==-1){
                //自行计算
                tar = secs[i]>0?Math.floor(second/secs[i]):second;
                second = second%secs[i];
            }
            if(isSlice){                
                if(tar>0){
                    fmt = replaceFmt2(reg[0],tar+"",fmt);
                }else{
                    fmt = fmt = sliceFmt(fmt,i+1,reg[0],regArr);
                }                
            }else{
                fmt = replaceFmt2(reg[0],tar+"",fmt);
            }
            
        }
    }
    return fmt;
}
function sliceFmt(fmt:string,j:number,curFmt:string,regArr:RegExp[]):string{
    let stIdx:number = fmt.indexOf(curFmt);
    let endIdx:number = -1;
    let nextReg:RegExp;
    let nextExc:RegExpExecArray;
    do{
        nextReg = regArr[j];                                        
        if(nextReg){
            nextReg.lastIndex = 0;
            nextExc = nextReg.exec(fmt);
            if(nextExc){
                endIdx = fmt.indexOf(nextExc[0]);
                break;
            }
        }
        j++;
    }while(nextReg);
    let fix:string = fmt.slice(0,stIdx);
    let end:string = endIdx>-1?fmt.slice(endIdx):"";
    fmt =  fix + end;
    return fmt;
}

function replaceFmt2(regStr: string, ydStr: string, fmt: string): string {
    if (regStr.length > ydStr.length) {
        ydStr = "0" + ydStr;
    }
    fmt = fmt.replace(regStr, ydStr);
    return fmt;
}

DateTimeUtils.format(86436,"hh:ii:ss");
DateTimeUtils.format(new Date(1579334657*1000),"Y年MM月DD日 hh时ii分ss秒");
DateTimeUtils.format(new Date(),"Y年MM月DD日 hh时ii分ss秒");
DateTimeUtils.format(86421,"d天hh时ii分ss秒",true);
DateTimeUtils.format(86400,"ii分ss秒",true);
DateTimeUtils.format(86300,"d天hh时ii分ss秒",true);
DateTimeUtils.format(new Date(),"Y年MM月DD日 hh时ii分ss秒",true);

function getSectionValue(newValue:number):number{
    let _sectionVals:number[] = [30,40,50,100];
    //let _sectionVals:number[] = [7,14,21,28];
    let _sectionSizes:number[] = [35,65,85,100]; //每个值对应的宽度
    let maxW:number = _sectionSizes[_sectionSizes.length-1];;
    let maxVal:number = _sectionVals[_sectionVals.length-1];
    let len:number = _sectionVals.length;
    for (let i = 0; i < len; i++) {
        let sectionVal = _sectionVals[i];
        if (newValue<=sectionVal) {
            //开始计算当前位置
            let curW:number = _sectionSizes[i];
            let secWPer:number = curW/maxW;//宽占总数的百分比
            let newValSecPer:number = newValue/sectionVal;
            let per:number =newValSecPer*secWPer; //当前值占总宽的百分比
            newValue = per*maxVal; //总宽百分比等于总值百分比，所有得到新的总值
            console.log("newValue width:",per*maxW);
            break;
        }
        
    }
    return newValue;
}

console.log("分段计算:",getSectionValue(40));
let u8a:Uint8Array = new Uint8Array(2);
u8a[0] = "我".charCodeAt(0);
u8a[1] = "是".charCodeAt(0);
let encoder:TextEncoder = new TextEncoder()
u8a = encoder.encode("我是")
console.log("encoder",u8a);

let decoder:TextDecoder = new TextDecoder("utf-8");
console.log("decoder",decoder.decode(u8a),u8a.length,u8a.byteLength);

let byte:ByteArray = new ByteArray();
byte.writeInt16(1024);
byte.writeInt32(32);
byte.writeUTF("我是谁")
byte.writeInt32(61331)
byte.position = 0;
console.log("int read",byte.readInt16());
console.log("int read",byte.readInt32());
console.log("int read",byte.readUTF());
console.log("int read",byte.readInt32());

let arr:number[] = [1,2,3,4,5];

function insertAt(array: any[], index: number, object: any): void {
    let i: number;
    let length: number = array.length;

    if (index < 0)
        index += length + 1;
    if (index < 0)
        index = 0;

    for (i = index - 1; i >= length; --i)
        array[i] = null;

    for (i = length; i > index; --i)
        array[i] = array[i - 1];

    array[index] = object;
    
}

insertAt(arr,3,100);
console.log("arr",arr);
//let html_tag_reg:RegExp = /<([a-zA-Z]{1,}).*?([a-zA-Z].*?=.*?)>|<([a-zA-Z]{1,}).*?\/>/;
//let html_tag_reg:RegExp = /<[a-zA-Z]{1,}.*?>|<\/[a-zA-Z]{1,}.*?>/;
let html_tag_reg:RegExp = /<[a-zA-Z]{1,}.*?>|<\/[a-zA-Z]{1,}.*?>/g;
let htmlStr:string = "A<img src='/>B<font color='#00ff00'>C</font>D";

console.log("分割后2:",htmlStr.split(html_tag_reg),html_tag_reg.exec(htmlStr));
let obj:any = html_tag_reg.exec(htmlStr);
let c:number = 0;
while(obj){
    console.log("obj",obj);
    htmlStr = htmlStr.replace(obj[0],"");
    obj = html_tag_reg.exec(htmlStr);
    c++;
    if (c>=10) {
        break
    }
}
